package com.pozu.new_retail.controllers;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pozu.new_retail.dao.base.JsonResult;
import com.pozu.new_retail.dao.model.FavoriteRecord;
import com.pozu.new_retail.service.FavoriteRecordService;
import com.pozu.new_retail.utils.CodeConstants;
import com.pozu.new_retail.utils.CodeProperties;
import com.pozu.new_retail.utils.Tools;

@Controller
public class FavoriteRecordController {

    @Autowired
    private FavoriteRecordService favoriteRecordService;

    @ResponseBody
    @RequestMapping(value = "/api/favoriteRecords/list", method = RequestMethod.GET)
    public Object get(HttpServletRequest request) throws Exception {

        long userId = Long.parseLong(request.getHeader("userId"));

        int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序

        int type = Integer.parseInt(request.getParameter("type"));//默认0商品1ugc内容id

        int pageStart = Integer.parseInt(request.getParameter("pageStart"));//第几页从0开始
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));//一页显示几条数据

        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        map.put("pageStart", pageStart * pageSize);
        map.put("pageSize", pageSize);
        map.put("appId", appId);

        JsonResult jr = new JsonResult();

        jr.respCode = CodeConstants.SUCCESS_CODE;
        jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);

        if (type == 0) {
            jr.respData = favoriteRecordService.queryFavoriteGoodsByMap(map);
        } else if (type == 1) {
            jr.respData = favoriteRecordService.queryFavoriteUgcsByMap(map);
        }

        return jr;
    }

    @ResponseBody
    @RequestMapping(value = "/api/favoriteRecords", method = RequestMethod.POST)
    public Object save(HttpServletRequest request) throws Exception {

        int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序

        long userId = Long.parseLong(request.getHeader("userId"));

        FavoriteRecord requestFavoriteRecord = Tools.getRequestObject(request, FavoriteRecord.class);

        requestFavoriteRecord.setUserId(userId);

        JsonResult jr = new JsonResult();

        HashMap<String, Object> hashMap = new HashMap();
        hashMap.put("type", requestFavoriteRecord.getType());
        hashMap.put("userId", userId);
        hashMap.put("otherId", requestFavoriteRecord.getOtherId());
        hashMap.put("appId", appId);

        FavoriteRecord favoriteRecord = favoriteRecordService.queryByMap(hashMap);
        if (favoriteRecord != null) {
            if (favoriteRecord.getState() == 0) {
                //已收藏
                jr.respCode = CodeConstants.ALREADYFAVORITED_MSG;
                jr.respMsg = CodeProperties.getString(CodeConstants.ALREADYFAVORITED_MSG);
                return jr;
            } else {
                requestFavoriteRecord.setState(0);
                requestFavoriteRecord.setFavoritedId(favoriteRecord.getFavoritedId());
                favoriteRecordService.updateFavoriteRecord(requestFavoriteRecord);
            }
        } else {
            requestFavoriteRecord.setAppId(appId);
            requestFavoriteRecord.setUserId(userId);
            favoriteRecordService.saveFavoriteRecord(requestFavoriteRecord);
        }

        jr.respCode = CodeConstants.SUCCESS_CODE;
        jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);

        return jr;
    }

    @ResponseBody
    @RequestMapping(value = "/api/favoriteRecords", method = RequestMethod.PUT)
    public Object update(HttpServletRequest request) throws Exception {
        //取消收藏
        long userId = Long.parseLong(request.getHeader("userId"));

        FavoriteRecord requestFavoriteRecord = Tools.getRequestObject(request, FavoriteRecord.class);

        requestFavoriteRecord.setUserId(userId);

        JsonResult jr = new JsonResult();

        HashMap<String, Object> hashMap = new HashMap();
        hashMap.put("type", requestFavoriteRecord.getType());
        hashMap.put("userId", userId);
        hashMap.put("otherId", requestFavoriteRecord.getOtherId());

        FavoriteRecord record = favoriteRecordService.queryByMap(hashMap);

        if (null != record) {
            record.setState(1);
            favoriteRecordService.updateFavoriteRecord(record);
        }

        jr.respCode = CodeConstants.SUCCESS_CODE;
        jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
        return jr;
    }

}
